#include "MainUI.h"

void MainUI::m_slipAPt()
{
    if(m_isOpenSlip)
    {
        QValueAxis *axisX = dynamic_cast<QValueAxis*>(m_chart->axisX());//
        qreal cur_x_min = axisX->min();
        qreal cur_x_max = axisX->max();
        axisX->setRange(cur_x_min + 1, cur_x_max + 1);
    }
}

void MainUI::m_intiChart()
{
    /* 创建波形对象和波形视图，波形视图依附在波形对象上*/
    m_chart = new QChart();
    m_chartView = new QChartView(m_chart, ui->wOscilloscope);
    /* 开启动画*/
#if 0
    m_chart->setAnimationOptions(QChart::AllAnimations);
#endif
    
    /* 创建X轴 并设置范围 单位 轴名称*/
    m_axisX = new QValueAxis;
    m_axisX->setRange(-128, 128);
    m_axisX->setLabelFormat("%g");
    m_axisX->setTitleText("点击line(x)显示/隐藏线条");
    
    /* 创建Y轴 并设置范围 单位 轴名称*/
    m_axisY = new QValueAxis;
    m_axisY->setRange(-2, 2);
    m_axisY->setTitleText("纵轴值");
    m_chart->setAxisX(m_axisX);
    m_chart->setAxisY(m_axisY);
    
    /* 设置线条数 用对少根线分割坐标轴*/
    m_axisX->setTickCount(5);
    m_axisY->setTickCount(5);
    
    for(uint32_t i = 0 ;i<SERIE_CNT;++i)
    {
        m_addSeries();
    }
    
    /* 点击显示和不现实曲线*/
    m_connectMarkers();
    
    /* 创建主布局，栅格布局*/
    m_mainLayout = new QGridLayout();
    m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
    ui->wOscilloscope->setLayout(m_mainLayout);
    
    /* 设置波形图显示标题*/
    m_chart->setTitle("工欲善其事必先利其器 by luckyxrq  工程开源于 https://gitee.com/luckyxrq/qt-chart");
    
    /* 设置波形标签显示 & 显示位置*/
    m_chart->legend()->setVisible(true);
    m_chart->legend()->setAlignment(Qt::AlignBottom);
    
    /* 设置渲染器 抗锯齿*/
    m_chartView->setRenderHint(QPainter::Antialiasing);
    
    /* XY方向同时放大到鼠标画出的矩形大小（也可以设置为只放大X轴或Y轴）*/
    m_chartView->setRubberBand(QChartView::NoRubberBand);
}

void MainUI::m_addSeries()
{
    QSplineSeries *series = new QSplineSeries(m_chart);
    m_series.append(series);
    
    series->setName(QString("line " + QString::number(m_series.count())));
    
#if 0
    /* 创建一些默认数据显示*/
    QList<QPointF> data;
    int offset = m_chart->series().count();
    for (int i = 0; i < 50; i++) {
        qreal x = offset * 20 + i;
        data.append(QPointF(i, qSin(qDegreesToRadians(x))));
    }
    series->append(data);
#endif 
    
    m_chart->addSeries(series);
    
    /* 必须先将series添加到m_chart，下面的坐标对应才生效*/
    series->attachAxis(m_axisX);
    series->attachAxis(m_axisY);
    
}

void MainUI::m_connectMarkers()
{
    /* 获取所有的曲线的标签*/
    const auto markers = m_chart->legend()->markers();
    for (QLegendMarker *marker : markers) {
        /* 将曲线的点击先断开信号与槽 再连接*/
        QObject::disconnect(marker, &QLegendMarker::clicked,
                            this, &MainUI::m_handleMarkerClicked);
        QObject::connect(marker, &QLegendMarker::clicked, this, &MainUI::m_handleMarkerClicked);
    }
    
}

void MainUI::m_handleMarkerClicked()
{
    QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
    Q_ASSERT(marker);
    
    switch (marker->type())
    {
    case QLegendMarker::LegendMarkerTypeXY:
    {
        marker->series()->setVisible(!marker->series()->isVisible());
        marker->setVisible(true);
        qreal alpha = 1.0;
        
        if (!marker->series()->isVisible())
            alpha = 0.5;
        
        QColor color;
        QBrush brush = marker->labelBrush();
        color = brush.color();
        color.setAlphaF(alpha);
        brush.setColor(color);
        marker->setLabelBrush(brush);
        
        brush = marker->brush();
        color = brush.color();
        color.setAlphaF(alpha);
        brush.setColor(color);
        marker->setBrush(brush);
        
        QPen pen = marker->pen();
        color = pen.color();
        color.setAlphaF(alpha);
        pen.setColor(color);
        marker->setPen(pen);
        break;
    }
    default:
    {
        qDebug() << "Unknown marker type";
        break;
    }
    }
}

